package com.jcl.recruit.core.common.page;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jcl.core.common.page.EntityPage;
import com.jcl.core.common.page.PageResult;

import java.util.List;

import static org.springframework.beans.support.PagedListHolder.DEFAULT_PAGE_SIZE;

/**
 * @ClassName EntityPage
 * @Description 分页查询
 * @Author yyliu
 * @Date 2018/9/17 17:41
 * @Version 1.0
 **/
public abstract class AbstractEntityPage<T> implements EntityPage<T> {

    protected abstract List<?> searchConditionList(T entity);

    @Override
    public PageResult page(T entity, Integer curPage, Integer pageSize) {

        if(null == curPage){
            curPage = 1;
        }
        if(null == pageSize){
            pageSize = DEFAULT_PAGE_SIZE;
        }
        PageHelper.startPage(curPage, pageSize);
        List<?> entityList = searchConditionList(entity);
        PageInfo pageInfo = new PageInfo(entityList);
        PageResult result = new PageResult(curPage, pageSize, pageInfo.getTotal(),
                pageInfo.getList());
        return result;
    }

}
